package service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import service.tAverageDateService;
import util.DateUtil;
import dao.tAverageDateDao;
import entity.tAverageDate;

public class tAverageDateServiceImpl implements tAverageDateService
{
    private tAverageDateDao tAverageDateDao;
    
    private List<tAverageDateDao> list = new ArrayList<tAverageDateDao>();
    
    public String getDataByTime(String startime, String endtime, int mark, String deviceId)
    {
        
        String hql =
            "from tAverageDate  t where t.hTime >=cast('" + startime + "' as date) and t.hTime <=cast('" + endtime
                + "' as date) and t.mark=" + mark + " and t.deviceId='" + deviceId + "' order by t.hTime";
        
        List<tAverageDate> list = this.tAverageDateDao.queryString(hql);
        String date = "[";
        if (list != null)
        {
            for (int i = 0; i < list.size(); i++)
            {
                Float tempFloat = Float.parseFloat(list.get(i).gethData());
                
                switch (mark)
                {
                    case 0:
                        tempFloat = tempFloat / 100;
                        break;
                    case 1:
                        break;
                    case 2:
                        tempFloat = tempFloat / 10;
                        break;
                    case 3:
                        tempFloat = tempFloat / 10;
                        break;
                    default:
                        break;
                }
                
                if (i == list.size() - 1)
                {
                    date += "[" + i + "," + tempFloat + "]";
                }
                else
                {
                    date += "[" + i + "," + tempFloat + "],";
                }
            }
        }
        else
        {
            date += "[]";
        }
        date = date + "]";
        return date;
    }
    
    public tAverageDateDao gettAverageDateDao()
    {
        return tAverageDateDao;
    }
    
    public void settAverageDateDao(tAverageDateDao tAverageDateDao)
    {
        this.tAverageDateDao = tAverageDateDao;
    }
    
    public String getX(String startime, String endtime)
    {
        DateUtil du = new DateUtil();
        Date start = du.StringToDate(startime, "yyyy-MM-dd");
        long day = du.getDaySub(startime, endtime);
        SimpleDateFormat sf1 = new SimpleDateFormat("M-d");
        GregorianCalendar gc = new GregorianCalendar();
        String data = "[";
        int j = 0;
        for (int i = 0; i <= day; i++)
        {
            int index_left = i + j;
            int index_right = index_left + 1;
            j++;
            gc.setTime(start);
            gc.add(5, i);
            if (i == day)
            {
                data +=
                    "[\"" + index_left + "\"," + "\"" + sf1.format(gc.getTime()) + "\"],[\"" + index_right + "\","
                        + "\" \"]";
            }
            else
            {
                data +=
                    "[\"" + index_left + "\"," + "\"" + sf1.format(gc.getTime()) + "\"],[\"" + index_right + "\","
                        + "\" \"],";
            }
            
        }
        data = data + "]";
        // data="[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61]";
        return data;
    }
    
}
